<template>
  <div class="page">
    <van-cell-group>

      <van-field
        v-model="login.user"
        required
        clearable
        label="用户名"
        right-icon="question-o"
        placeholder="请输入用户名"
        @click-right-icon="$toast('question')"
      />

      <van-field
        v-model="login.pwd"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
      />

    </van-cell-group>

    <van-button type="primary"
      @click="handleLogin"
      :loading="loading"
      >登录
    </van-button>
  </div>
</template>

<script>
  import { login } from '@/api/index'
  export default {
    data() {
      return {
        login: {
          user: "xiaonan001",
          pwd: "a12345678",
        },
        loading: false,
      };
    },
    mounted() {
      const env = process.env.NODE_ENV
      if (env === 'development') {
        this.loginForm = {
          user: 'xiaonan001',
          pwd: 'a12345678'
        }
      }
    },
    methods: {
      handleLogin() {
        this.loading = true;
        let para = {
          username: this.login.user,
          password: this.login.pwd
        }
        login(para).then(res => {
          if (res.code === 200) {
            localStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          }
          this.loading = false
        }).catch(() => {
          this.$toast('登录失败')
          this.loading = false;
        })
      }
    }
  };
</script>

<style lang="scss" scoped>
.page {
  padding: 10px;
}
</style>
